package com.haolijun.eecms.controller;

import cn.hutool.json.JSONUtil;
import com.haolijun.eecms.common.R;
import com.haolijun.eecms.controller.form.InsertActiveForm;
import com.haolijun.eecms.controller.form.SearchActiveByPageForm;
import com.haolijun.eecms.entity.TbActive;
import com.haolijun.eecms.service.ActiveService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;

/**
 * @ClassName ActiveController
 * @Description TODO
 * @Author hljstart
 * @Date 2023/3/12 10:17
 * @Version 1.0
 */
@RestController
@RequestMapping("/active")
@Tag(name = "ActiveController",description = "活动Web接口")
public class ActiveController {

    @Autowired
    private ActiveService activeService;

    @PostMapping("/searchActiveByPage")
    @Operation(summary = "查询活动分页详情")
    public R searchActiveByPage(@Valid @RequestBody SearchActiveByPageForm from){
        HashMap param = JSONUtil.parse(from).toBean(HashMap.class);
        R r = activeService.searchActiveByPage(param);
        return r;
    }

    @PostMapping("/insert")
    @Operation(summary = "发布活动")
    public R insert(@Valid @RequestBody InsertActiveForm form){
        TbActive active = JSONUtil.parse(form).toBean(TbActive.class);
        int rows = activeService.insert(active);
        return rows > 0 ? R.ok() : R.error();
    }

    @GetMapping("/delete/{id}")
    @Operation(summary = "删除活动")
    public R delete(@PathVariable("id") Integer id){
        int rows = activeService.delete(id);
        return rows > 0 ? R.ok() : R.error();
    }

    @GetMapping("/searchActiveInfo/{id}")
    @Operation(summary = "获取活动的详细信息")
    public R searchActiveInfo(@PathVariable("id") Integer id){
        HashMap map = activeService.searchActiveInfo(id);
        return R.ok().data(map);
    }
}
